package com.mjfeed.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mjfeed.bean.AreaJSON;
import com.mjfeed.util.DBHelper;


/**
 * 地区Dao
 * @author zhaominglei
 * @date 2014-12-11
 * 
 */
public class AreaDao extends BaseDao {
	private static final String TABLE_NAME = "feed_area";
	
	public int getCount(Context context) {
		return getCount(context, TABLE_NAME);
	}
	
	// TODO 如果用到 更新 treepath
	public void save(Context context, AreaJSON areaJSON) {
		SQLiteDatabase db = DBHelper.getInstance(context).getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put("areaid", areaJSON.getAreaId());
		contentValues.put("name", areaJSON.getName());
		contentValues.put("fullname", areaJSON.getFullName());
		contentValues.put("orders", areaJSON.getOrders());
		contentValues.put("parentid", areaJSON.getParentId());
		db.insert(TABLE_NAME, null, contentValues);
	}
	
	public void delete(Context context, Long areaId) {
		SQLiteDatabase  db = DBHelper.getInstance(context).getWritableDatabase();
		db.execSQL("delete from "+TABLE_NAME+" where areaid=?", new Object[]{ areaId });
	}
	
	public void deleteAll(Context context) {
		deleteAll(context, TABLE_NAME);
	}
	
	public List<AreaJSON> getChildren(Context context, Long parentId) {
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = null;
		if (parentId == null || parentId <= 0) {
			cursor = db.rawQuery("select * from "+TABLE_NAME+" where parentid is NULL", null);
		} else {
			cursor = db.query(TABLE_NAME, null, "parentid=?", new String[]{ String.valueOf(parentId) }, null, null, null);
		}
		List<AreaJSON> areaList = new ArrayList<AreaJSON>();
		while (cursor.moveToNext()) {
			AreaJSON areaJSON = new AreaJSON();
			areaJSON.setAreaId(cursor.getLong(cursor.getColumnIndex("areaid")));
			areaJSON.setOrders(cursor.getInt(cursor.getColumnIndex("orders")));
			areaJSON.setFullName(cursor.getString(cursor.getColumnIndex("fullname")));
			areaJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			areaJSON.setTreePath(cursor.getString(cursor.getColumnIndex("treepath")));
			areaJSON.setParentId(cursor.getLong(cursor.getColumnIndex("parentid")));
			areaList.add(areaJSON);
		}
		cursor.close();
		return areaList;
	}
	
	public AreaJSON getById(Context context, Long areaId) {
		SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from "+TABLE_NAME+" where areaid=?", new String[]{ String.valueOf(areaId) } );
		AreaJSON areaJSON = null;
		if (cursor.moveToFirst()) {
			areaJSON = new AreaJSON();
			areaJSON.setAreaId(areaId);
			areaJSON.setFullName(cursor.getString(cursor.getColumnIndex("fullname")));
			areaJSON.setName(cursor.getString(cursor.getColumnIndex("name")));
			areaJSON.setOrders(cursor.getInt(cursor.getColumnIndex("orders")));
			areaJSON.setParentId(cursor.getLong(cursor.getColumnIndex("parentid")));
			areaJSON.setTreePath(cursor.getString(cursor.getColumnIndex("treepath")));
		}
		cursor.close();
		return areaJSON;
	}
}
